import hashlib
import hmac
import json
import time
from datetime import datetime, timezone
from hashlib import sha256
import binascii
import requests

# from logger import logger

# test环境 云游戏AK/SK
# cgiaas
# AK = "lLESMOaehAibxc5i"
# SK = "EPxBifrGZ1nGUxya"

# Cloudgame
AK = "l0lsgFT8ZrZ5Gqys"
SK = "qAfbUn1dEMDQTxJf"

httpRequestMethod = "POST"
# 测试环境
# url = 'http://iaas-api-test.icloudsky.com/api/v1'
# 预发布环境
url = 'https://iaas-api-pre.icloudsky.com/api/v1'
# url = 'http://iaas-api.icloudsky.com/api/v1'

# 正式环境
# url = "https://iaas-api.icloudsky.com/api/v1"

# action = "DescribeInstance"
# action = "DescribeSystemImage"
# action = "CreateInstance"
# payload = {
#     "InstanceIDs": ["ins-gwnmbczrsbfjfflbunradseolmgmnvnv"],
#     # "ServerIDs": ["hyp-alxjbsQIfgDTMICpgBrpSMuJs"],
#     # "Arch": "x86",
#     # "BusinessStatus": "InstanceNormal",
#     # "PageNumber": 1,
#     # "PageSize": 20
# }

#
payload = {
    "IDC": "cn-east-hefei-3009", # 机房id
    "Amount": 1,  # 实例数量
    "Spec": "egc-x866248ra-v1",  # 规格
    "SystemImageID": "os-hg8anl58c990o9bz9g0omf4ccz9", # 系统镜像 ID
    "SystemDisk": {"DiskType": "Cloud", "DiskStructure": "Snapshot", "WritingMode": "WriteToLocal"} #系统盘规格
}


action = "CreateInstance"
# 自建arm 三开
# payload = {
#                 "IDC": "cn-north-neimeng-3000",
#                 "Amount": 3,
#                 "Spec": "egc-armg-v3",
#                 "SystemImageID": "cimg-7PNUHvmzaRkavHDD",
#            }
# 阿里arm 双开
# payload = {
#                 "IDC": "cn-south-foshan-03009",
#                 "Amount": 2,
#                 "Spec": "egc-armg2m1a-v2",
#           "AutoUseCoupon": True,
#                 "SystemImageID": "cimg-448a2788f99ccdd218f0aeb001d29777",
#             "Namespace": "iaas-test"
#          }

# 阿里arm 单开
# payload = {
#                 "IDC": "cn-south-foshan-03009",
#                 "Amount": 1,
#                 "Spec": "egc-armg2m1a-v1",
#           "AutoUseCoupon": True,
#                 "SystemImageID": "cimg-448a2788f99ccdd218f0aeb001d29777",
#             "Namespace": "iaas-test"
#          }
def sha256_hash(message):
    sha256 = hashlib.sha256()
    sha256.update(message.encode('utf-8'))
    hash_value = sha256.hexdigest()
    return hash_value


def hmac_sha256(s, key):
    s = s.encode('utf-8')
    sign = hmac.new(key, s, digestmod=sha256).digest()
    return sign


def hex_encode_to_string(byte_string):
    return binascii.hexlify(byte_string).decode('utf-8')


payload_str = json.dumps(payload)
print(payload)
print(url)
hash_value = sha256_hash(payload_str)
canonicalRequest = httpRequestMethod + "\n" + action + "\n" + hash_value
# print(canonicalRequest)
hashedCanonicalRequest = sha256_hash(canonicalRequest)
# print(hashedCanonicalRequest)
TS = time.time()
str_TS = str(TS)
TS_end = str_TS[0:10]
# TS_end = "1710245927"
# print(TS_end)
TraceID = "flow_" + str(TS_end)
# print(TraceID)
now = datetime.now(timezone.utc)
Date = now.strftime('%Y-%m-%d')
# print("当前的UTC标准日期是:", Date)
string2sign = TS_end + "\n" + Date + "/yt_request" + "\n" + hashedCanonicalRequest
# print(string2sign)
SecretDate = hmac_sha256(Date, ("YT" + SK).encode('utf-8'))
# print(SecretDate)
SecretSigning = hmac_sha256("yt_request", SecretDate)
# print(SecretSigning)
signature = hmac_sha256(string2sign, SecretSigning)
signature_end = hex_encode_to_string(signature)
# print("sing:", signature_end)

# 如果你需要发送JSON数据
headers = {'X-ICS-Action': action, 'X-ICS-AK': AK, 'X-ICS-TimeStamp': TS_end, 'Content-Type': 'application/json',
           'X-ICS-Sign': signature_end, 'X-ICS-TraceID': TraceID}
response = requests.post(url, data=json.dumps(payload), headers=headers)
# 打印响应状态码和内容
print(response.status_code)
# print(response.text)
pretty_response = json.dumps(response.json(), indent=4)
print(pretty_response)
